#include <iostream>
#include <string>
#include <set>
#include <vector>

void parseStr(std::string str, std::vector<int> &nums)
{
    int i = 0;
    while (i < str.size())
    {
        auto pos = str.find(',', i);
        if (pos == std::string::npos)
        {
            std::string tmp_str = str.substr(i);
            nums.push_back(std::stoi(tmp_str));
            i = str.size();
        }
        else
        {
            std::string tmp_str = str.substr(i, pos - i);
            nums.push_back(std::stoi(tmp_str));
            i = pos + 1;
        }
    }
}

int main()
{
    int seat_num;
    std::cin >> seat_num;
    std::string str;
    std::cin >> str;

    std::vector<int> nums;
    parseStr(str, nums);

    int last_seat = -1;
    std::set<int> seats;
    for (int i = 0; i < nums.size(); i++)
    {
        if (nums[i] == 1)
        {
            if (seats.size() == seat_num)
            {
                // std::cout << seat_num << " " << seats.size() << std::endl;
                std::cout << -1 << std::endl;
                return 0;
            }

            int seat = -1, max_dist = -1;
            if (seats.empty())
            {
                seat = 0;
                last_seat = seat;
            }
            else
            {
                int prev = -1;
                for (int j = 0; j < seat_num; j++)
                {
                    if (seats.count(j))
                    {
                        prev = j;
                    }
                    else
                    {
                        int dist;
                        if (prev == -1)
                        {
                            dist = j;
                        }
                        else
                        {
                            dist = (j - prev + 1) / 2;
                        }
                        if (dist > max_dist)
                        {
                            max_dist = dist;
                            seat = prev + dist;
                        }
                    }
                }

                if (!seats.count(seat_num - 1))
                {
                    int dist = seat_num - 1 - *seats.rbegin();
                    if (dist > max_dist)
                    {
                        seat = seat_num - 1;
                    }
                }
            }
            last_seat = seat;
            seats.insert(seat);
        }
        else
        {
            seats.erase(-nums[i]);
        }
    }
    std::cout << last_seat << std::endl;
    return 0;
}